#!/bin/bash
#name:discuz
#自动化安装apache，mysql，php并且部署discuz论坛
#2024/07/04写入
#存放于/root/discuz.sh

Mysql_Pass=root

#关闭SELINUX
setenforce 0
sed -ri '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config &> /dev/null
#关闭防火墙
systemctl stop firewalld &> /dev/null
#更新yum源
	yum clean all
	mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
	wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
	wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
	yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm -y 
	yum-config-manager --enable remi-php74
	yum makecache
#检查服务器有没有安装apache，mysql，php如果没有安装，直接安装

rpm -qa |grep httpd &> /dev/null
if [ $? != 0 ]; then
        echo "未安装apache,现在帮您安装"
        yum install httpd -y
        echo "安装完成，正在启动"
        #设置开机自启动
        systemctl start httpd
        systemctl enable httpd
        echo "已设置开机自启动"
fi

rpm -qa |grep mariadb-server &> /dev/null
if [ $? != 0  ]; then
	echo "未安装mysql,现在帮您安装"
	yum -y install mariadb-server mariadb
	echo "安装完成，正在启动"
	#设置开机自启
	systemctl start mariadb
	systemctl enable mariadb

	mysqladmin -uroot password "$Mysql_Pass"
	mysql -uroot -p"$Mysql_Pass" -e "create database  discuz"
fi

rpm -qa |grep php php-mysqlnd &>/dev/null

if [ $? != 0  ]; then
	echo "未安装php，现在帮您安装"
	# 这里安装了PHP及其与MySQL的交互模块
	yum install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json -y
	echo "PHP 安装完成"
fi
# 重启Apache服务  
systemctl restart httpd  

#准备部署

if [ ! -e /root/download ]; then
	wget https://gitee.com/Discuz/DiscuzX/attach_files/1309965/download
fi
yum install -y unzip
if [ ! -d /root/upload ]; then
	unzip -o download
fi
if [ ! -f /var/www/html/index.php ]; then
	cp -rf upload/* /var/www/html
	chown -R  apache.apache  /var/www/html
fi

systemctl restart httpd mariadb


